﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Churchtoolkit.Utils.BackgroundTasks;
using Utils.EventAggregator;
using Churchtoolkit.Utils.RulesManager;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.Utils.EmailProvider;
using System.Web;

namespace Churchtoolkit.Web.Shell.Services.Bootstrappers
{
    public class RegistrerdUserMailBootstraperTask : IBackgroundTask
    {
        IEventAggregator _eventAggregator;
        IEmailProvider _emailProvider;
        public RegistrerdUserMailBootstraperTask(IEventAggregator eventAggregator,IEmailProvider emailProvider)
        {
            _eventAggregator = eventAggregator;
            _emailProvider = emailProvider;
        }
        #region IBackgroundTask Members

        public void Start()
        {
            _eventAggregator.GetEvent<EntityAction>().Subscribe<Members>(e =>
                {
                    if (e.State == EntityState.Inserted)
                    {
                        _emailProvider.SendMail(e.Entity.Email, "0001", e.Entity.FirstName, System.Web.VirtualPathUtility.ToAbsolute(
                            string.Format("~/Activate.aspx?i={0}", e.Entity.MemberId.ToString().Encrypt(Constants.ApplicationKey))));
                    }
                });
        }

        public void Stop()
        {

        }

        #endregion
    }
}
